<template>
    <div>

        <div class="item">
            <el-input size="large" v-model="params.provinceTitle" readonly :placeholder="$t('account.local')"
                suffix-icon="ArrowRightBold" @click="handleOpenProvince" :disabled="disabled"></el-input>
        </div>
        <div class="item">
            <el-input size="large" v-model="params.cityTitle" readonly :placeholder="$t('account.area1')"
                suffix-icon="ArrowRightBold" @click="handleOpenCity" :disabled="disabled"></el-input>
        </div>
        <div class="item">
            <el-input size="large" v-model="params.areaTitle" readonly :placeholder="$t('account.area2')"
                suffix-icon="ArrowRightBold" @click="handleOpenArea" :disabled="disabled"></el-input>
        </div>
        <div class="item">
            <el-input size="large" :disabled="disabled" v-model="params.addressDetail" type="textarea" rows="4"
                maxlength="200" show-word-limit :placeholder="$t('account.detail')" suffix-icon="ArrowRightBold"></el-input>
        </div>



        <!-- 省 -->
        <w-pop ref="popProvince" :direction="'btt'" :showHeader="false">
            <div class="popProvince_list">
                <div class="item" v-for="item in list" :key="item.id" @click="handleClickAddr(item)">{{ item.name }}</div>
            </div>
        </w-pop>
    </div>
</template>

<script>
import { defineComponent, ref, watch, watchEffect } from "vue";
import { getLocalArea, addUserAddr } from '@/api/user'
import { ElMessage } from "element-plus";
import { useI18n } from 'vue-i18n'
export default defineComponent({
    props: {
        disabled: {
            type: Boolean,
            default: () => false
        }
    },
    components: {},
    emits: ['succ', 'ouput'],
    setup(props, { emit }) {
        const { t } = useI18n()
        const pop = ref();
        // 编辑对象
        const item = ref(null)
        // 参数
        const params = ref({
            province: "",
            provinceTitle: "",
            city: "",
            cityTitle: "",
            area: "",
            areaTitle: "",
            addressDetail: "",
        })
        const open = (val) => {
            if (val) {
                console.log(val)
                item.value = val
                for (var k in params.value) {
                    params.value[k] = val[k]
                }
            }
            pop.value.open();
        };

        const handleClose = () => {
            pop.value.close();
        }


        const popProvince = ref()
        const type = ref(0)
        const list = ref([])
        const handleOpenProvince = async () => {
            popProvince.value.open()
            type.value = 0
            const res = await getLocalArea({ pid: '' })
            if (res) {
                list.value = res
            }
        }
        const handleOpenCity = async () => {
            if (!params.value.province) {
                ElMessage({
                    type: 'warning',
                    message: t("tips.province")
                })
                return
            }
            popProvince.value.open()
            type.value = 1
            const res = await getLocalArea({ pid: params.value.province })
            if (res) {
                list.value = res
            }
        }
        const handleOpenArea = async () => {
            if (!params.value.city) {
                ElMessage({
                    type: 'warning',
                    message: t("tips.city")
                })
                return
            }
            popProvince.value.open()
            type.value = 2
            const res = await getLocalArea({ pid: params.value.city })
            if (res) {
                list.value = res
            }
        }

        const handleClickAddr = (item) => {
            if (type.value == 0) {
                params.value.provinceTitle = item.name
                params.value.province = item.id
                params.value.cityTitle = ''
                params.value.city = ''
                params.value.areaTitle = ''
                params.value.area = ''
            } else if (type.value == 1) {
                params.value.cityTitle = item.name
                params.value.city = item.id
                params.value.areaTitle = ''
                params.value.area = ''
            } else {
                params.value.areaTitle = item.name
                params.value.area = item.id
            }

            popProvince.value.close()
            emit('ouput', params.value)

        }
        watch(() => params.value.addressDetail, (v) => {
            emit('ouput', params.value)
        })


        return {
            item,
            params,
            list,
            pop,
            open,
            handleClose,
            popProvince,
            handleOpenProvince,
            handleClickAddr,
            handleOpenCity,
            handleOpenArea,
        };
    },
});
</script>

<style scoped lang="scss">
.item {
    padding: 6px 0px;

    .label {
        color: #999;
        font-family: Helvetica;
        font-size: 12px;
        font-style: normal;
        font-weight: 400;
        line-height: normal;
        margin-bottom: 4px;
    }
}

.comfir_btn {
    margin: 24px 16px;

    button {
        width: 100%;
        height: 48px;
        border-radius: 50px;
    }
}

.popProvince_list {
    max-height: 300px;
    overflow-y: auto;

    .item {
        font-size: 14px;
        height: 30px;
        line-height: 30px;
        text-align: center;
        border-bottom: 1px solid rgba(204, 204, 204, 0.485);
    }
}


.close_icon {
    position: absolute;
    right: 16px;
    top: -80px;
    z-index: 10000;
}
</style>
